// util/upload.js
const multer = require('multer');
const path = require('path');

// 设置存储路径和文件名
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'public/uploads'); // 文件保存到 public/uploads/
  },
  filename: function (req, file, cb) {
    // 文件名：字段名+时间戳+扩展名
    const ext = path.extname(file.originalname);
    cb(null, file.fieldname + '-' + Date.now() + ext);
  }
});

// 导出上传中间件
const upload = multer({ storage });

module.exports = upload;
